| Example Program Dijkstras Algorithm Computing single source shortest paths using Dijkstra algorithm. This code example illustrates the dijkstra's algorithm, once using an external map and once using an internal map.
1 | #include <seqan/graph_algorithms.h>
| 2 | #include <iostream>
| 3 |
| 4 | using namespace seqan;
| 5 |
| 6 | int main() {
| 7 | typedef Graph<Directed<> > TGraph;
| 8 | typedef VertexDescriptor<TGraph>::Type TVertexDescriptor;
| 9 | typedef EdgeDescriptor<TGraph>::Type TEdgeDescriptor;
| 10 | typedef Size<TGraph>::Type TSize;
|
11 | TSize numEdges = 10;
| 12 | TVertexDescriptor edges[] = {0,1, 0,3, 1,2, 1,3, 2,4, 3,1, 3,2, 3,4, 4,0, 4,2};
| 13 | TGraph g;
| 14 | addEdges(g, edges, numEdges);
| 15 | std::cout << g << ::std::endl;
|
16 | unsigned int weights[] = {10, 5, 1, 2, 4, 3, 9, 2, 7, 6};
| 17 | String<unsigned int> weightMap;
| 18 | resizeEdgeMap(g,weightMap, weights);
|
19 | String<unsigned int> predMap;
| 20 | String<unsigned int> distMap;
|
21 | dijkstra(g,0,weightMap,predMap,distMap);
|
22 | std::cout << "Single-Source Shortest Paths: " << ::std::endl;
| 23 | typedef Iterator<TGraph, VertexIterator>::Type TVertexIterator;
| 24 | TVertexIterator it(g);
| 25 | while(!atEnd(it)) {
| 26 | std::cout << "Path from 0 to " << getValue(it) << ": ";
| 27 | _print_path(g,predMap,(TVertexDescriptor) 0, getValue(it));
| 28 | std::cout << " (Distance: " << getProperty(distMap, getValue(it)) << ")" << ::std::endl;
| 29 | goNext(it);
| 30 | }
|
31 | typedef unsigned int TEdgeCargo;
| 32 | typedef Directed<TEdgeCargo> TEdges;
| 33 | typedef Graph<TEdges> TCargoGraph;
|
34 | TCargoGraph cargo_g;
| 35 | addEdges(cargo_g, edges, numEdges);
| 36 | std::cout << cargo_g << ::std::endl;
|
37 | InternalMap<TEdgeCargo> intMap;
| 38 | resizeEdgeMap(cargo_g,intMap, weights);
|
39 | clear(predMap);
| 40 | clear(distMap);
|
41 | dijkstra(cargo_g,0,intMap,predMap,distMap);
|
42 | std::cout << "Single-Source Shortest Paths: " << ::std::endl;
| 43 | typedef Iterator<TCargoGraph, VertexIterator>::Type TCargoVertexIterator;
| 44 | TCargoVertexIterator itC(cargo_g);
| 45 | while(!atEnd(itC)) {
| 46 | std::cout << "Path from 0 to " << getValue(itC) << ": ";
| 47 | _print_path(cargo_g,predMap,(TVertexDescriptor) 0, getValue(itC));
| 48 | std::cout << " (Distance: " << getProperty(distMap, getValue(itC)) << ")" << ::std::endl;
| 49 | goNext(itC);
| 50 | }
| 51 | return 0;
| 52 | }
|
|